
package com.hulk.ratel.common.security;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;
import java.security.*;

/**
* @author cmt  
* @E-mail:29572320@qq.com
* @version Create on:  2017年4月28日 上午11:32:07
* Class description
*/

public class ConsumeSecurityWarper {
	

	
	/**
	 * 用partnerAESKey加密请求报文
	 * @param plainText
	 * @param AESkey
	 * @return
	 */
	public static String  getCipherB64PlainTextByPartnerAESKey(String plainText,String AESkey){
		//构建
		//SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, AESkey.getBytes(StandardCharsets.UTF_8));
		AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,AESkey.getBytes(StandardCharsets.UTF_8));
		//加密
		String encrypt = aes.encryptBase64(plainText);
		return encrypt;
		
	}
	
	
	
	/**
	 * 用partnerPriKey签名请求报文 signtData
	 * @param plainText
	 * @param partnerPrivateKey
	 * @return RSA.digitalPrivateKeySign(text, privatekeypartner);
	 */
	public static String getSignB64PlainTextByPartnerPrivateKey(String plainText , PrivateKey partnerPrivateKey ){

//		Sign sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA);
//		Signature signature = null;
//		try {
//			signature = Signature.getInstance(SignAlgorithm.SHA256withRSA.getValue());
//			signature.initSign(partnerPrivateKey);
//		} catch (NoSuchAlgorithmException e) {
//			e.printStackTrace();
//			throw  new RuntimeException(e);
//		}catch (InvalidKeyException e) {
//			e.printStackTrace();
//			throw  new RuntimeException(e);
//		}
//		sign.setSignature(signature);
//        //签名
//		byte[] signed = sign.sign(plainText.getBytes(StandardCharsets.UTF_8));
//		return new String (signed,StandardCharsets.UTF_8);
		try {
			Signature signature = Signature.getInstance(SignAlgorithm.SHA256withRSA.getValue());
			signature.initSign(partnerPrivateKey);
			signature.update(plainText.getBytes(StandardCharsets.UTF_8));
			byte[] signBytes = signature.sign();

			return Base64.encode(signBytes);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(String.format("数字签名时没有[%s]此类算法", SignAlgorithm.SHA256withRSA.getValue()),e);
		} catch (InvalidKeyException e) {
			throw new RuntimeException("数字签名时私钥无效",e);
		} catch (SignatureException e) {
			throw new RuntimeException("数字签名时出现异常",e);
		}

	}
	
	
	
	/**
	 * 用platformPubKey加密partnerAESKey
	 * @return
	 */
	public static String getCipherB64AESKeyByPlatformPublicKey(String partnerAESKey, PublicKey platformPublicKey){
		 RSA  rsa = new RSA();

		 rsa.setPublicKey(platformPublicKey);
		return  rsa.encryptBase64(partnerAESKey, KeyType.PublicKey);
		
	}

	/**
	 * 生成随机密钥
	 *
	 * @param size
	 *            位数
	 * @return
	 */
	public static String generateRandomKey(int size) {
		StringBuilder key = new StringBuilder();
		String chars = "0123456789ABCDEF";
		for (int i = 0; i < size; i++) {
			int index = (int) (Math.random() * (chars.length() - 1));
			key.append(chars.charAt(index));
		}
		return key.toString();
	}


}
